package org.jsbd.boss.common.mbean.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.mbean.ControllerParameterMBean;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.service.SimulatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ControllerParameter implements ControllerParameterMBean {
	private static final Logger logger = LoggerFactory.getLogger(ControllerParameter.class);

	@Resource
	private SimulatorManager simulatorManager;

	@Autowired
	private RedisClient<String, Object> redisClient;

	private int recordNum = EXTRACT_RECORD_NUM;

	@Override
	public void put(String key, String value) {
		redisClient.put(key, value);
	}

	@Override
	public void setRecordNum(int recordNum) {
		this.recordNum = recordNum;
	}

	@Override
	public int getRecordNum() {
		return this.recordNum;
	}

	@Override
	public void reloadOnlineLoginData() {
	}

	/**
	 * 获取redis队列大小
	 */
	@Override
	public long getQueueSize(String key) {
		return redisClient.getQueueSize(key);
	}

	/**
	 * 删除redis队列
	 */
	@Override
	public void relushRedisQueue(String key) {
		redisClient.remove(key);
		long size = redisClient.getQueueSize(key);
		logger.info("队列  {}  长度为 {} ", new Object[] { key, size });
	}

	/**
	 * 获取有序队列长度
	 */
	@Override
	public long getZsetSize(String key) {
		// TODO Auto-generated method stub
		return redisClient.getZsetsize(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.common.mbean.ControllerParameterMBean#getSetSize(java.lang
	 * .String)
	 */
	@Override
	public String getSet(String key) {
		return redisClient.getSet(key).toString();
	}

	@Override
	public String evalGet(String key) {
		return (String) redisClient.evalGet(key);
	}

	@Override
	public Boolean evalSet(String key, String value) {
		return redisClient.evalSet(key, value);
	}

	@Override
	public Set keys(String keys) {
		if (StringUtils.isNotBlank(keys) && keys.equals("*")) {
			return null;
		}
		return redisClient.keys(keys);
	}

	@Override
	public String get(String key) {
		return (String) redisClient.get(key);
	}

	@Override
	public String getObject(String key) {
		return JSONObject.toJSONString(redisClient.get(key));
	}

	@Override
	public String evalHget(String key, String field) {
		return redisClient.evalHget(key, field);
	}

	@Override
	public Map<String,Integer> getOnlineSimulators(Boolean isBusy) {
		return simulatorManager.getOnlineSimulators(isBusy);
	}

	@Override
	public long ttl(String key) {
		return redisClient.ttl(key);
	}

	@Override
	public void remove(String key) {
		redisClient.remove(key);
	}

	@Override
	public Long evalHincrby(String key, String field, int incryBy) {
		return redisClient.evalHincrby(key, field, incryBy);
	}

	@Override
	public void evalHset(final String key, final String field, String value) {
		redisClient.evalHset(key, field, value);
	}

	@Override
	public void hset(final String key, final String field, String value) {
		redisClient.hset(key, field, value);
	}

	@Override
	public String hget(final String key, final String field) {
		Object obj = redisClient.hget(key, field);
		if (null != obj) {
			return obj.toString();
		}
		return "";
	}

	@Override
	public Set hkeys(String key) {
		if (StringUtils.isNotBlank(key) && key.equals("*")) {
			return null;
		}
		return redisClient.hkeys(key);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.common.mbean.ControllerParameterMBean#removeOfPre(java.
	 * lang.String)
	 */
	@Override
	public void redisRemoveOfPre(String key) {
		if (StringUtils.isNotBlank(key) && key.equals("*")) {
			return;
		} else {
			Set<String> keys = redisClient.keys(key);
			for (String temp : keys) {
				redisClient.remove(temp);
			}
		}

	}


	@Override
	public void hdel(String key, String field) {
		redisClient.hdel(key, field);
	}

	@Override
	public List<String> getList(String key) {
		return (List<String>) redisClient.getList(key);
	}

}
